﻿/// * Write a program that sorts an array of strings using the quick sort algorithm (find it in Wikipedia).


using System;

class zad14
{
    static void Main()
    {
        int[] arr = { 1, 3, 5, 2, 7 };
        int[] res = QuickSort(arr);
        //Console.WriteLine();
    }

    public static int[] QuickSort(int[] arr)
    {
        int[] result = QuickSort(arr, 0, arr.Length - 1);
        return result;
    }
    private static int[] QuickSort(int[] arr, int left, int right)
    {
        int leftIndex = left;
        int rightIndex = right;
        int pivotIndex = (left + right) / 2;
        int pivotValue = arr[pivotIndex];
        int temp = 0;
        
        while (leftIndex <= rightIndex)
        {
            while (arr[leftIndex] < pivotValue)
            {
                leftIndex++;
            }
            
            while (pivotValue < arr[rightIndex])
            {
                rightIndex--;
            }
            
            
            if (leftIndex <= rightIndex)
            {
                temp = arr[leftIndex];
                arr[leftIndex] = arr[rightIndex];
                leftIndex++;
                arr[rightIndex] = temp;
                rightIndex--;
            }
        }
        
        if (left < rightIndex)
        {
            QuickSort(arr, left, rightIndex);
        }
        
        if (leftIndex < right)
        {
            QuickSort(arr, leftIndex, right);
        }
        return arr;
    }

}

